package com;

import com.fz.entity.Student;
import com.fz.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * Created by webrx on 2017-08-23.
 */
public class Demo {
    protected SqlSessionFactory sf;
    protected SqlSession session;
    @Before
    public void init() {
        try {
            this.sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
            this.session = this.sf.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void add(){
        StudentMapper sdao = this.session.getMapper(StudentMapper.class);
        sdao.insert(new Student("李四",60,"郑州"));
        sdao.insert(new Student("赵冰",70,"北京"));
        sdao.insert(new Student("李丰",80,"上海"));
        sdao.insert(new Student("张三丰",20,"天津"));
        sdao.insert(new Student("jack",90,"开封"));
        sdao.insert(new Student("Mary",92,"洛阳"));
    }


    @Test
    public void show() {
        StudentMapper sdao = this.session.getMapper(StudentMapper.class);
        Student st = sdao.queryById(10);
        System.out.println(st);
    }

    @After
    public void close() {
        if(this.session!=null){
            this.session.commit();
            this.session.clearCache();
            this.session.close();
        }
    }

}
